<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>模板编辑</title>
    <#include "/pubHeadStyle.html">
</head>
<body>
<form class="layui-form" style="background: #fff;height:99%; padding-top: 4px; padding-right: 6px" lay-filter="templateForm">
    <div>
    <div class="layui-form-item item-first">
        <label class="layui-form-label">模板名称<span style="color:red">*</span></label>
        <div class="layui-input-block" >
            <input type="text" name="name" lay-verify="name" autocomplete="off"
                   class="layui-input">
            <input type="hidden" name="templateId" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">使用类型</label>
        <div class="layui-input-block">
            <select name="useType" lay-filter="useType" lay-verify="required">
                <option value="1">终止提醒</option>
                <option value="2">催办提醒</option>
                <option value="3">审批提醒</option>
                <option value="4">撤销提醒</option>
                <option value="5">取消转办</option>
                <option value="6">沟通提醒</option>
                <option value="7">归档提醒</option>
                <option value="8">转办提醒</option>
                <option value="9">退回提醒</option>
                <option value="10">被沟通人提交</option>
                <option value="11">取消代理</option>
                <option value="12">抄送提醒</option>
                <option value="13">流程节点无人员</option>
                <option value="14">跟进事项预警</option>
                <option value="15">跟进事项到期</option>
                <option value="16">跟进事项完成&评价</option>
                <option value="17">跟进事项通知</option>
                <option value="10">跟进事项已评价知会</option>
                <option value="19">逾期提醒</option>
                <option value="22">代理提醒</option>
                <option value="23">消息转发</option>
                <option value="24">重启任务</option>
                <option value="25">通知任务所属人(代理)</option>
                <option value="26">加签提醒</option>
                <option value="27">加签反馈</option>
                <option value="28">取消流转</option>
                <option value="29">补签提醒</option>
                <option value="30">创建人消息提醒</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item item-first">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block" >
            <textarea id="title" name="title" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item item-first">
        <label class="layui-form-label">富文本内容</label>
        <div class="layui-input-block" >
            <textarea id="htmlContent" name="htmlContent" style="display: none;" lay-verify="htmlContent"></textarea>
        </div>
    </div>
    <div class="layui-form-item item-first">
        <label class="layui-form-label">普通内容</label>
        <div class="layui-input-block" >
            <textarea id="plainContent" name="plainContent" class="layui-textarea"></textarea>
        </div>
    </div>
    </div>
    <input type="hidden" name="isDefault" lay-verify="isDefault">
    <div class="layui-btn-container" style="position: absolute;width: 100%;text-align: center;bottom: 10px;">
        <button class="layui-btn" lay-submit lay-filter="templateFormSave">保存</button>
        <button class="layui-btn layui-btn-danger" id="return">取消</button>
    </div>
</form>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form','layedit','setter','commons'],function(){
        var layedit = layui.layedit,
            form = layui.form,
            setter = layui.setter,
            commons = layui.commons,
            $ = layui.$;


        var id='${templateId}';
        if(id != null && id != ''){
            $.ajax({
                url: serverPath.workflowPath  + setter.workflowLinkPath + "sysTemplate/get",
                type: "get",
                dataType: 'json',
                data: {templateId:id},
                async:false,
                success: function (data) {
                    var returnDate = data.data;
                    form.val('templateForm',
                        {
                            "templateId": returnDate.templateId,
                            "name": returnDate.name,
                            "useType": returnDate.useType,
                            "title": returnDate.title,
                             // "htmlContent": layui.layedit.setContent(index,),//富文本获取初始值
                            "plainContent": returnDate.plainContent,
                            "isDefault":returnDate.isDefault
                        })
                    $('#htmlContent').html(returnDate.htmlContent)

                }
            });
        }

        var index = layedit.build('htmlContent', {
            tool: [
                'strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'|' //分割线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'link' //超链接
                ,'unlink' //清除链接
                ,'face' //表情
            ],
            height: 100 //设置编辑器高度
        }); //建立编辑器
        //自定义验证规则,富文本内容同步到表单
        form.verify({
            htmlContent: function(value){
                return layedit.sync(index);
            }
            ,nameMaxLength: [
                /^[\S]{1,16}$/
                ,'模板名称最多输入16位，且不能出现空格'
            ]

        });

        form.render();
        //监听提交
        form.on('submit(templateFormSave)', function (data) {
            $.ajax({
                url: serverPath.workflowPath + setter.workflowLinkPath + "sysTemplate/checkRepeatName",
                type: 'post',
                data: {
                    id:id
                    , name: data.field.name
                },
                dataType: 'json',
                success: function (result) {
                    if (result.code == 0) {
                        $.ajax({
                            url: serverPath.workflowPath + setter.workflowLinkPath + "sysTemplate/save",
                            type: "POST",
                            data: data.field,
                            dataType:'json',
                            success: function (result) {
                                if (commons._filterRecord(result)) {
                                    layer.alert(result.msg, function (index) {
                                        layer.close(index);
                                        parent.layui.table.reload('noteTemplate', {
                                            page: {curr: 1}
                                        });
                                        parent.layui.totalFilter.init('noteTemplate');
                                        parent.layer.close(index1);
                                    });
                                } else {
                                    layer.alert(result.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                layer.alert("保存失败！" + XMLHttpRequest.status);
                            }
                        });
                    }else{
                        //名称已存在
                        parent.layer.msg(result.msg, {time: 2500, icon: 5});
                    }
                }
            });
            return false;
        });


        var index1 = parent.layer.getFrameIndex(window.name);
        $('#return').click(function () {
            parent.layer.close(index1);
        })
    })
</script>
</body>
</html>